package com.elite.groovy.davco.bak

import com.elite.groovy.callback.ServiceBackInvoke
import com.elite.groovy.kh.KHDBConfig
import com.elite.groovy.kh.KHUtil
import com.elite.groovy.util.EliteBean
import com.elite.groovy.util.Util
import net.sf.json.JSONArray
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.jdbc.core.JdbcTemplate

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 * @projectName: Groovy
 * @package: com.elite.groovy.callback
 * @className: autoSMSTemplate
 * @description: 自动同步云片短信模板
 * @author: LiJT
 * @date: 2023-10-27 11:13
 * @version: 1.0
 */

public class autoSMSTemplate implements ServiceBackInvoke {
    private static final Logger log = LoggerFactory.getLogger(autoSMSTemplate.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        try {
            JdbcTemplate jt = new JdbcTemplate(eliteBean.getDataSource());

            // 调用地址
            String sendURL = KHDBConfig.getAPIURL(eliteBean, "YUNPIAN-MSGTEMPLATE_URL").replace("#{apikey}", KHDBConfig.getParamConfig(eliteBean, "YUNPIAN_TZ_apiKey"));
            log.debug("调用地址:" + sendURL);

            // 处理头文件
            Map<String, String> headerMap = new HashMap<String, String>();
            headerMap.put("Accept", "application/json;charset=utf-8");
            headerMap.put("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
            log.debug("调用头文件:" + headerMap);

            String postResult = Util.doHttpPostUseHead(sendURL, "", "UTF-8", 120000, 0, headerMap);
            log.debug("调用返回结果：" + postResult);

            JSONArray templateArr = JSONArray.fromObject(postResult);

            for (int i = 0; i < templateArr.size(); i++) {

                String tpl_id = KHUtil.GetSFJV(templateArr.getJSONObject(i), "tpl_id");
                String tpl_content = KHUtil.GetSFJV(templateArr.getJSONObject(i), "tpl_content");
                String check_status = KHUtil.GetSFJV(templateArr.getJSONObject(i), "check_status");
                String reason = KHUtil.GetSFJV(templateArr.getJSONObject(i), "reason");
                String lang = KHUtil.GetSFJV(templateArr.getJSONObject(i), "lang");
                String country_code = KHUtil.GetSFJV(templateArr.getJSONObject(i), "country_code");
                log.debug("处理模板tpl_id:" + tpl_id + " tpl_content:" + tpl_content);

                String dealSQL = "select * from Doctemplate where tpl_id = ?";
                List<Map<String, Object>> selectCus = jt.queryForList(dealSQL, tpl_id);

                log.debug("selectCus：" + selectCus);
                log.debug("检查数据(" + tpl_id + ") 数量：" + selectCus.size());

                if (selectCus.size() == 0) {
                    String insertSQL = '''Insert Into Doctemplate
                                                  (Doctemplate_Id,
                                                   Docgroup_Id,
                                                   Description,
                                                   Documentpath,
                                                   Documentpath2,
                                                   Status,
                                                   Mailmerge,
                                                   Doctype,
                                                   Printtype,
                                                   Dynid,
                                                   Default_Flag,
                                                   Tpl_Id,
                                                   Check_Status,
                                                   Reason)
                                                Values
                                                  (?,
                                                   'XXXXXX',
                                                   ?,
                                                   ?,
                                                   ?,
                                                   1,
                                                   Null,
                                                   0,
                                                   'S',
                                                   Null,
                                                   Null,
                                                   ?,
                                                   ?,
                                                   ?)''';
                    int num = jt.update(insertSQL, KHUtil.getRandomString(6).toUpperCase(), tpl_content.replace("【西卡德高】", "").substring(0, tpl_content.replace("【西卡德高】", "").length() > 20 ? 20 : tpl_content.replace("【西卡德高】", "").length() - 1), tpl_content.replace("【西卡德高】", ""), tpl_content, tpl_id, check_status, reason);
                    log.debug("新增数据(" + tpl_id + ") 数量：" + num);
                } else {
                    String updateSQL = '''Update Doctemplate
                                           Set Documentpath  = ?,
                                               Documentpath2 = ?,
                                               Status = Case
                                                          When ? = 'SUCCESS' Then
                                                           1
                                                          Else
                                                           0
                                                        End,
                                               Check_Status  = ?,
                                               Reason        = ?
                                         Where Tpl_Id = ?''';
                    int num = jt.update(updateSQL, tpl_content.replace("【西卡德高】", ""), tpl_content, check_status, check_status, reason, tpl_id);
                    log.debug("更新数据(" + tpl_id + ") 数量：" + num);
                }

            }

        } catch (Exception e) {
            log.error("自动获取短信平台模板异常-:" + e.getMessage(), e);
        }
    }
}